package com.cyh.testdesignpattern.mediator;

import android.util.Log;

import java.util.HashMap;

//具体的中介者
public class ConcreteMediator extends Mediator {
    private HashMap<String, Colleague> colleagueMap;
    private HashMap<String, String> interMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);
        if(colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if(colleague instanceof Curtains) {
            interMap.put("Curtains", colleagueName);
        }
    }

    @Override
    public void getMessage(int stateChange, String colleagueName) {
        if(colleagueMap.get(colleagueName) instanceof  Alarm) {
            //说明闹钟处理不了消息，转发了
            if(stateChange == 0) {
                ((Curtains)colleagueMap.get(interMap.get("Curtains"))).downCurtains();
            } else if(stateChange == 1) {
                ((Curtains)colleagueMap.get(interMap.get("Curtains"))).upCurtains();
            }
        } else if(colleagueMap.get(colleagueName) instanceof  Curtains) {
            //如果窗帘也处理不了消息转发了，因为是最后一个，所以给一个提示
            Log.i("Concretemediator", "----最后这个消息处理不了");
        }
    }

    @Override
    public void sendMessage() {

    }
}
